package com.fingard.dsp.bank.directbank.cib01;


import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillQuery;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.*;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.*;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created by F on 2018/9/11.
 */
@Component("CIB01ElecBill")
public class CIB01ElecBill extends CIB01Base implements IElecBillQuery {

    @Override
    public RetAtsB001OperBillQry D_OperBill_Query_Req(ReqB001OperBillQry query) throws Exception {
        /**
         * 注意！！兴业银行，该接口与票据池B301接口兴业银行可操作票据查询接口使用的是同一个银行提供的接口
         * 修改时注意两个接口的实现类都要进行修改
         */
        RetAtsB001OperBillQry retAts = new RetAtsB001OperBillQry(query.reqHeader);
        retAts.respCode = "0";
        retAts.writeBefore();
        try {
            while (query.readRecord()) {
//                    <BUSINESSTYPE/>	业务编码
//                1-出票 2-承兑 3-收票 4-背书转让 5-质押 6-解质押 7-出票保证 8-承兑保证 9-背书保证 10-贴现 11-回购式贴现赎回 12-提示付款 13-追索 14-同意清偿
                String businessType;
                int busiType = Integer.valueOf(query.detail.busiType);
                if ("1".equals(query.detail.queryType)) {
                    switch (busiType) {
                        case 1:businessType="01";break;//出票
                        case 2:businessType="03";break;//承兑
                        case 3:businessType="05";break;//收票
                        case 4:businessType="07";break;//背书
                        case 5:businessType="15";break;//质押
                        case 10:businessType="09";break;//贴现
                        case 12:businessType="17";break;//提示付款
                        case 13:if("1".equals(query.detail.recourseType)){
                            businessType = "19";//拒付
                        } else {
                            businessType = "20";//非拒付
                        }break;//追索
                        case 14:businessType="21";break;//清偿
                        default: throw  new Exception("未知的业务类型："+query.detail.busiType);
                    }
                } else if ("2".equals(query.detail.queryType)) {
                    switch (busiType) {
                        case 2:businessType="03";break;//承兑
                        case 3:businessType="05";break;//收票
                        case 4:businessType="07";break;//背书
                        case 6:businessType="16";break;//押解
                        case 11:businessType="10";break;//贴现
                        case 14:businessType="21";break;//同意清偿
                        default:throw new Exception("未知的业务类型:"+query.detail.busiType);
                    }
                } else if ("3".equals(query.detail.queryType)) {
                    switch (busiType) {
                        case 1:businessType="06";break;//撤票
                        case 2:businessType="22";break;//承兑撤回
                        case 3:businessType="23";break;//收票撤回
                        case 4:businessType="24";break;//背书撤回
                        case 5:businessType="33";break;//质押撤回
                        case 10:businessType="25";break;//贴现撤回
                        case 12:businessType="35";break;//提示付款撤回
                        case 13:businessType="38";break;//追索通知撤回
                        case 14:businessType="37";break;//同意清偿撤回
                        default:throw new Exception("未知的业务类型:"+query.detail.busiType);
                    }
                } else {
                    throw new Exception("未知的查询类型：" + query.detail.queryType);
                }

                if ("2".equals(query.detail.queryType)) {
                    queryOperBill_ebsignquerytrnrq(retAts, query.detail, businessType);
                } else {
                    queryOperBill_eboprnquerytrnrq(retAts, query.detail, businessType);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }
        retAts.writeEnd();
        return retAts;
    }
    //可操作票据查询
    public void queryOperBill_eboprnquerytrnrq(RetAtsB001OperBillQry retAts, ReqB001OperBillQryDetail reqDetail, String buisType) throws Exception {
        boolean more = true;
        int page = 1;
        while (more) {
            more = false;
            XmlTextWriter xmlWriter = new XmlTextWriter();
            xmlWriter.autoFormat = false;
            writeCommonHeaderEn(xmlWriter, "encoding=\"gb2312\"");
//            < SECURITIES_MSGSRQV1>	请求服务指令消息集	必输
            xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
//                    <EBOPRNQUERYTRNRQ>	服务标识	必输
            xmlWriter.writeStartElement("EBOPRNQUERYTRNRQ");
//            <TRNUID/>	客户端交易的唯一流水号
            String trnuId = getQryTransID();
            xmlWriter.writeElementString("TRNUID", trnuId);
//            建议值为YYYYMMDD+序号，最长30位	必输
//            <RQBODY PAGE=“1”>	消息报文体,PAGE页码数	必输
            xmlWriter.writeElementAttribute("RQBODY", "PAGE", page + "");
//            <BUSINESSTYPE/>	业务编码
            xmlWriter.writeElementString("BUSINESSTYPE", buisType);
//            参见附录《电子票据系统编码对照表》	必输
//                    <BILLCODE/>	票据号码	非必输
            if (StringHelper.hasAnyChar(reqDetail.billNo)) {
                xmlWriter.writeElementString("BILLCODE", reqDetail.billNo);
            }
//            <ACCTID/>	查询账户代号	必输
            xmlWriter.writeElementString("ACCTID", reqDetail.actNum);
            if (StringHelper.hasAnyChar(reqDetail.dueDateDown) || StringHelper.hasAnyChar(reqDetail.dueDateUp)) {
                //            <DUEDATE>	票据到期日日期范围	非必输
                xmlWriter.writeStartElement("DUEDATE");
                //                    <DTSTART/>
                xmlWriter.writeElementString("DTSTART", reqDetail.dueDateDown);
                //            <DTEND/>
                xmlWriter.writeElementString("DTEND", reqDetail.dueDateUp);
                //            </DUEDATE>
                xmlWriter.writeEndElement();
            }
            if (StringHelper.hasAnyChar(reqDetail.billAmtDown) || StringHelper.hasAnyChar(reqDetail.billAmtUp)) {
                //            <BILLAMT>	票据金额范围	非必输
                xmlWriter.writeStartElement("BILLAMT");
                //                    <MINAMT/>
                xmlWriter.writeElementString("MINAMT", reqDetail.billAmtDown);
                //            <MAXAMT/>
                xmlWriter.writeElementString("MAXAMT", reqDetail.billAmtUp);
                //            </BILLAMT>
                xmlWriter.writeEndElement();
            }
//            </>	票据种类BILLTYPE	非必输
            String billType;
            if ("1".equals(reqDetail.billType)) {
                billType = "AC01";
            } else {
                billType = "AC02";
            }
            xmlWriter.writeElementString("BILLTYPE", billType);
//                    </RQBODY>
//            </EBOPRNQUERYTRNRQ>
//            </ SECURITIES_MSGSRQV1>
            xmlWriter.writeEndDocument();
            String xmlSend = writeContentLength(xmlWriter);
            String[] result = sendToBank(xmlSend);
            if (result[0].length() == 0) {
                String returnXml = result[1].substring(result[1].indexOf("<FOX>"), result[1].length());
                StringBuilder tmpSbmsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbmsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBOPRNQUERYTRNRS/STATUS/CODE");
                    String returnMsg = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBOPRNQUERYTRNRS/STATUS/MESSAGE");
                    if ("0".equals(returnCode)) {
                        if (tmpBkReader.read("/FOX/SECURITIES_MSGSRSV1/EBOPRNQUERYTRNRS/RSBODY")) {
                            String moreStr = tmpBkReader.readedXElement.getAttrValue("MORE");
                            if (StringHelper.hasAnyChar(moreStr) && moreStr.equals("Y")) {
                                more = true;
                                page++;
                            } else {
                                more = false;
                            }
                        } else {
                            return;
                        }
                        ArrayList<XElement> list = tmpBkReader.readedXElement.getChildList("CONTENT");
                        if (list != null) {
                            for (XElement xElement : list) {
                                RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
                                //电子票据号码
                                detail.billNo = xElement.getSingleText("BILLCODE");
                                //票据类型 1-银承 2-商承
                                String billType2 = xElement.getSingleText("BILLTYPE");
                                if ("AC01".equals(billType2)) {
                                    detail.billType = "1";
                                } else if ("AC02".equals(billType2)) {
                                    detail.billType = "2";
                                }
                                //出票日
                                detail.issDate = xElement.getSingleText("DRAWDATE");
                                //到期日
                                detail.dueDate = xElement.getSingleText("DUEDATE");
                                //电子票据金额
                                detail.billAmt = xElement.getSingleText("BILLAMT");
                                String transFlag = xElement.getSingleText("NEGTBL");
                                //背书转让标识 1-可再转让 0-不得转让
                                if ("EM00".equals(transFlag)) {
                                    detail.transFlag = "1";
                                } else if ("EM01".equals(transFlag)) {
                                    detail.transFlag = "0";
                                }
                                //出票人
                                detail.drawName = xElement.getSingleText("DRAWERACCT/NAME");
                                //出票人账号
                                detail.drawAct = xElement.getSingleText("DRAWERACCT/ACCTID");
                                //出票人开户行行号
                                detail.drawBnkCode = xElement.getSingleText("DRAWERACCT/BANKNUM");
                                //承兑人名称
                                detail.accpName = xElement.getSingleText("ACPTRACCT/NAME");
                                //承兑人账号
                                detail.accpAct = xElement.getSingleText("ACPTRACCT/ACCTID");
                                //承兑人开户行行号
                                detail.accpBnkCode = xElement.getSingleText("ACPTRACCT/BANKNUM");
                                //收款人
                                detail.payeeName = xElement.getSingleText("PAYEEACCT/NAME");
                                //收款人账号
                                detail.payeeAct = xElement.getSingleText("PAYEEACCT/ACCTID");
                                //收款人开户行行号
                                detail.payeeBnkCode = xElement.getSingleText("PAYEEACCT/BANKNUM");
                                //交易明细流水
                                String seq = xElement.getSingleTextNotNull("SEQ");
                                String bizflag = xElement.getSingleTextNotNull("BIZFLAG");
                                detail.busiNo = seq + "@" + bizflag;
                                //操作标志
                                detail.busiType = reqDetail.busiType;
                                detail.queryType = reqDetail.queryType;

                                detail.applyReq = xElement.getSingleText("APPDATE");
                                //票据状态
                                detail.billStatus = xElement.getSingleText("BILLSTATUS");
                                retAts.writeDetail(detail);
                            }
                        }
                    } else {
                        throw new Exception(returnMsg);
                    }
//                    <EBOPRNQUERYTRNRS>
//                    <TRNUID>20120504cx10</TRNUID>
//                    <STATUS>
//                    <CODE>0</CODE>
//                    <SEVERITY>INFO</SEVERITY>
//                    <MESSAGE>处理成功！</MESSAGE>
//                    </STATUS>
//                    <RSBODY MORE="N">

                } else {
                    throw new Exception(tmpSbmsg.toString());
                }
            } else {
                throw new Exception(result[1]);
            }
        }
    }

    //查询可签收的票据
    public void queryOperBill_ebsignquerytrnrq(RetAtsB001OperBillQry retAts, ReqB001OperBillQryDetail reqDetail, String buisType) throws Exception {
        boolean more = true;
        int page = 1;
        while (more) {
            more = false;
            XmlTextWriter xmlWriter = new XmlTextWriter();
            xmlWriter.autoFormat = false;
            writeCommonHeaderEn(xmlWriter, "encoding=\"gb2312\"");
//            < SECURITIES_MSGSRQV1>	请求服务指令消息集	必输
            xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
//                    <EBSIGNQUERYTRNRQ>		必输
            xmlWriter.writeStartElement("EBSIGNQUERYTRNRQ");
//                    <TRNUID/>	客户端交易的唯一流水号
            String trnuId = getQryTransID();
            xmlWriter.writeElementString("TRNUID", trnuId);
//            建议值为YYYYMMDD+序号，最长30位	必输
//            <RQBODY PAGE=“1”>	PAGE查询页码，必输	必输
            xmlWriter.writeElementAttribute("RQBODY", "PAGE", page + "");
//            <BUSINESSTYPE/>	业务种类，见附录3	必输
            xmlWriter.writeElementString("BUSINESSTYPE", buisType);
//            <BILLCODE/>	票据号码,30位	非必输
//            xmlWriter.writeElementString("BILLCODE",reqDetail.billNo);
//            <ACCTID/>	操作员账户代号（将进行签收的操作账号）
            xmlWriter.writeElementString("ACCTID", reqDetail.actNum);
//            最大32位	必输
//            <APPDTSTART>	对方行业务申请日期范围
//                    <DTSTART/>	起始日期 YYYY-MM-DD
//                    <DTEND/>	结束日期 YYYY-MM-DD
//                    </APPDTSTART>
            xmlWriter.writeStartElement("APPDTSTART");
            Date now = new Date();
            String endDate = Format.Date10Format.format(now);
            int month = getBankConfig().getValueAsInt("monthInterval");
            if (month == -1) {
                month = -3;
            } else {
                month = 0 - month;
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(now);
            calendar.add(Calendar.MONTH, month);
            xmlWriter.writeElementString("DTSTART", Format.Date10Format.format(calendar.getTime()));
            xmlWriter.writeElementString("DTEND", endDate);
            xmlWriter.writeEndElement();

//            <BILLAMT>	票据金额范围	非必输
            if (StringHelper.hasAnyChar(reqDetail.billAmtUp) && StringHelper.hasAnyChar(reqDetail.dueDateDown)) {
                xmlWriter.writeStartElement("BILLAMT");
//                    <MINAMT/>	最小金额,17位(15整数位,2小数位)
                xmlWriter.writeElementString("MINAMT", reqDetail.billAmtDown);
//                    <MAXAMT/>	最大金额,17位(15整数位,2小数位)
                xmlWriter.writeElementString("MAXAMT", reqDetail.billAmtUp);
//                    </BILLAMT>
                xmlWriter.writeEndElement();
            }
//            <BILLTYPE/>	票据类型(AC01:银票,AC02:商票)	非必输
            String billType;
            if ("1".equals(reqDetail.billType)) {
                billType = "AC01";
            } else {
                billType = "AC02";
            }
            xmlWriter.writeElementString("BILLTYPE", billType);
//                    </RQBODY>
//            </EBSIGNQUERYTRNRQ>
//            </SECURITIES_MSGSRSV1>
            xmlWriter.writeEndDocument();
            String xmlSend = writeContentLength(xmlWriter);
            String[] result = sendToBank(xmlSend);
            if (result[0].length() == 0) {
                String returnXml = result[1].substring(result[1].indexOf("<FOX>"), result[1].length());
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbMsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBSIGNQUERYTRNRS/STATUS/CODE");
                    String returnMsg = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBSIGNQUERYTRNRS/STATUS/MESSAGE");
                    if ("0".equals(returnCode)) {
                        if (tmpBkReader.read("/FOX/SECURITIES_MSGSRSV1/EBSIGNQUERYTRNRS/RSBODY")) {
                            String moreStr = tmpBkReader.readedXElement.getAttrValue("MORE");
                            if (StringHelper.hasAnyChar(moreStr) && moreStr.equals("Y")) {
                                more = true;
                                page++;
                            } else {
                                more = false;
                            }
                        } else {
                            return;
                        }
                        ArrayList<XElement> list = tmpBkReader.readedXElement.getChildList("CONTENT");
                        if (list != null) {
                            for (XElement xElement : list) {
                                RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
//
//                                        <BUSINESSTYPE/>	业务类型
//                                        <BILLCODE/>	30位票据号码
                                detail.billNo = xElement.getSingleText("BILLCODE");
//                                        <BILLTYPE/>	票据类型,4位(AC01:银票,AC02:商票)
                                billType = xElement.getSingleTextNotNull("BILLTYPE");
                                if ("AC01".equals(billType)) {
                                    detail.billType = "1";
                                } else if ("AC02".equals(billType)) {
                                    detail.billType = "2";
                                }
//                                <BILLAMT/>	票据金额,17位(15整数位,2小数位)
                                detail.billAmt = xElement.getSingleTextNotNull("BILLAMT");
//                                        <DRAWDATE/>	出票日期
                                detail.issDate = xElement.getSingleText("DRAWDATE");
//                                        <DUEDATE/>	票据到期日
                                detail.dueDate = xElement.getSingleText("DUEDATE");
//                                        <NEGTBL/>	可否转让标识,
//                                        4位(EM00:可再转让,EM01:不可再转让)
                                String transFlag = xElement.getSingleText("NEGTBL");
                                if ("EM00".equals(transFlag)) {
                                    detail.transFlag = "1";
                                } else if ("EM01".equals(transFlag)) {
                                    detail.transFlag = "0";
                                }
//                                <BILLSTATUS/>	票据状态,6位见附录：票据状态表
                                detail.billStatus = xElement.getSingleText("BILLSTATUS");
//                                        <DRAWERACCT>	出票人账户信息
//                                <ACCTID/>	账户代号,最大32位
                                detail.drawAct = xElement.getSingleText("DRAWERACCT/ACCTID");
//                                        <NAME/>	账户名称,最大60位
                                detail.drawName = xElement.getSingleText("DRAWERACCT/NAME");
//                                        <BANKNUM/>	开户行号,最大12位
                                detail.drawBnkCode = xElement.getSingleText("DRAWERACCT/BANKNUM");
//                                        <ORGID/>	组织机构代码,最小1位,最大10位
                                detail.drawCode = xElement.getSingleText("DRAWERACCT/ORGID");
//                                        </DRAWERACCT>
//                                <ACPTRACCT>	承兑人账户信息
//                                        <ACCTID/>	账户代号,最大32位
                                detail.accpAct = xElement.getSingleText("ACPTRACCT/ACCTID");
//                                        <NAME/>	账户名称,最大60位
                                detail.accpName = xElement.getSingleText("ACPTRACCT/NAME");
//                                        <BANKNUM/>	开户行号,最大12位
                                detail.applyBnkCode = xElement.getSingleText("ACPTRACCT/BANKNUM");
//                                        </ACPTRACCT>
//                                <PAYEEACCT>	收款人账户信息
//                                        <ACCTID/>	账户代号,最大32位
                                detail.payeeAct = xElement.getSingleText("PAYEEACCT/ACCTID");
//                                        <NAME/>	账户名称,最大60位
                                detail.payeeName = xElement.getSingleText("PAYEEACCT/NAME");
//                                        <BANKNUM/>	开户行号,最大12位
                                detail.payeeBnkCode = xElement.getSingleText("PAYEEACCT/BANKNUM");
//                                        </PAYEEACCT>
//                                <BIZFLAG/>	商票系统业务标识号
                                detail.busiNo = xElement.getSingleText("BIZFLAG");
//                                        <ENTRSPAY/>	到期无条件支付委托
                                String mustPay = xElement.getSingleText("");
                                if ("CC00".equals(mustPay)) {
                                    detail.mustPay = "1";
                                } else {
                                    detail.mustPay = "0";
                                }
//                                        <APPDATE/>	业务申请日期
                                detail.applyDate = xElement.getSingleText("APPDATE");
//                                        <PROPSRINFO>	业务申请人信息
//                                        <ACCTID/>	账户代号,最大32位
                                detail.applyAct = xElement.getSingleText("PROPSRINFO/ACCTID");
//                                        <NAME/>	账户名称,最大60位
                                detail.applyName = xElement.getSingleText("PROPSRINFO/NAME");
//                                        <BANKNUM/>	开户行号,最大12位
                                detail.applyBnkCode = xElement.getSingleText("PROPSRINFO/BANKNUM");
//                                        <ORGID/>	组织机构代码,最小1位,最大10位
                                detail.applyCode = xElement.getSingleText("PROPSRINFO/ORGID");
//                                        </PROPSRINFO>
//                                <RCVRRINFO>	业务接收人信息
//                                        <ACCTID/>	账户代号,最大32位
                                detail.receAct = xElement.getSingleText("RCVRRINFO/ACCTID");
//                                        <NAME/>	账户名称,最大60位
                                detail.receName = xElement.getSingleText("RCVRRINFO/NAME");
//                                        <BANKNUM/>	开户行号,最大12位
                                detail.receBnkCode = xElement.getSingleText("RCVRRINFO/receBnkCode");
//                                        <ORGID/>	组织机构代码,最小1位,最大10位
                                detail.receCode = xElement.getSingleText("RCVRRINFO/ORGID");
//                                        </RCVRRINFO>
//                                <DISCNTTYPE/>	贴现类型,4位(RM00:买断式,RM01回购式)
                                String discType = xElement.getSingleText("DISCNTTYPE");
                                if ("RM00".equals(discType)) {
                                    detail.discType = "1";
                                } else if ("RM01".equals(discType)) {
                                    detail.discType = "2";
                                }
//                                <DISCNTINTRSTRATE/>	贴现利率,7位百分数不含百分号
                                detail.discRate = xElement.getSingleText("DISCNTINTRSTRATE");
//                                        (2位整数,5位小数)
//                                        <REDMDATE>	回购日期范围
//                                        <DTSTART/>	起始日期,YYYY-MM-DD
                                detail.repOpenDate = xElement.getSingleText("REDMDATE/DTSTART");
//                                        <DTEND/>	结束日期,YYYY-MM-DD
                                detail.repDueDate = xElement.getSingleText("REDMDATE/DTEND");
//                                        </REDMDATE>
//                                <REDMINTRSTRATE/>	赎回利率,7位百分数不含百分号(2位整数,5位小数)
                                detail.repRate = xElement.getSingleText("REDMINTRSTRATE");
//                                        <LQMODE/>	清算模式,4位
//                                        (SM00:线上清算,SM01:线下清算)
                                String payType = xElement.getSingleText("");
                                if ("SM00".equals(payType)) {
                                    detail.payType = "1";
                                } else if ("SM01".equals(payType)) {
                                    detail.payType = "2";
                                }
//                                        <RECRSAMT/>	追索金额,17位(15整数位,2小数位)
                                detail.recourseAmt = xElement.getSingleText("RECRSAMT");
//                                        <MEMO/>	备注,最大256位
                                detail.memo = xElement.getSingleTextNotNull("MEMO");
//                                        <OVERDUEREASON/>	逾期原因	非必回
                                detail.overDueRea = xElement.getSingleTextNotNull("OVERDUEREASON");
//                                        <TRANSCONTRACENO/>	交易合同号	非必回
                                detail.contractNo = xElement.getSingleTextNotNull("TRANSCONTRACENO");
//                                </CONTENT>
//                                </RSBODY>
//                                </EBSIGNQUERYTRNRS>
//                                </SECURITIES_MSGSRSV1>
                                retAts.writeDetail(detail);
                            }
                        }
                    } else {
                        throw new Exception(returnMsg);
                    }
//                    <EBOPRNQUERYTRNRS>
//                    <TRNUID>20120504cx10</TRNUID>
//                    <STATUS>
//                    <CODE>0</CODE>
//                    <SEVERITY>INFO</SEVERITY>
//                    <MESSAGE>处理成功！</MESSAGE>
//                    </STATUS>
//                    <RSBODY MORE="N">

                } else {
                    throw new Exception(tmpSbMsg.toString());
                }
            } else {
                throw new Exception(result[1]);
            }
        }
    }

    //票据明细查询
    @Override
    public RetAtsB002BillTransQry D_B002_BillTrans_Query(ReqB002BillTransQry query) throws Exception {
//        RetAtsB002BillTransQry retAts = new RetAtsB002BillTransQry(query.reqHeader);
//        retAts.respCode ="0";
//        retAts.respInfo ="";
//        retAts.writeBefore();
//        String respCode="";
//        String respInfo="";
//        try{
//             int page =0;
//             boolean more =true;
//             while (more) {
//                 XmlTextWriter xmlWriter = new XmlTextWriter();
//                 xmlWriter.autoFormat = false;
//                 writeCommonHeaderEn(xmlWriter, "encoding=\"gb2312\"");
////                < SECURITIES_MSGSRQV1>		必输
//                 xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
////                        <EBSTMTQUERYTRNRQ>		必输
//                 xmlWriter.writeStartElement("EBSTMTQUERYTRNRQ");
////                        <TRNUID/>	客户端交易的唯一流水号
//                 String trunId = getQryTransID();
//                 xmlWriter.writeElementString("TRNUID", trunId);
////                建议值为YYYYMMDD+序号，最长30位	必输
////                <RQBODY PAGE=“1”>	PAGE查询页码，必输	必输
//                 xmlWriter.writeElementAttribute("RQBODY", "PAGE", "" + page);
////                <OPERTYPE/>	发起方标志
////                01:发起方/02接收方/03:持票人/04:全部	必输
//                 xmlWriter.writeElementString("OPERTYPE", "");
////                <ACCTID/>	32位操作员账户代号	必输
//                 xmlWriter.writeElementString("ACCTID", query.actNum);
////                <BUSINESSTYPE/>	业务标识码,2位,见:附录3-业务类型对照表	必输
//                 xmlWriter.writeElementString("BUSINESSTYPE", "");
////                        <BILLCODE/>	票据号码,30位	非必输
//                 xmlWriter.writeElementString("BILLCODE", query.billNo);
////                </EBSTMTQUERYTRNRQ>
////                </ SECURITIES_MSGSRQV1>
//                 xmlWriter.writeEndDocument();
//                 String xmlSend = writeContentLength(xmlWriter);
//                 String [] result = sendToBank(xmlSend);
//                 if (result[0].length() == 0) {
//                     String returnXml = result[1].substring(result[1].indexOf("<FOX>"), result[1].length());
//                     StringBuilder tmpSbmsg =new StringBuilder();
//                     XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml,tmpSbmsg);
//                     if (tmpBkReader != null) {
//                         String returnCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBSTMTQUERYTRNRS/STATUS/CODE");
//                         String returnMsg = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBSTMTQUERYTRNRS/STATUS/MESSAGE");
//                         if ("0".equals(returnCode)) {
//                             if (tmpBkReader.read("/FOX/SECURITIES_MSGSRSV1/EBSTMTQUERYTRNRS/RSBODY")) {
//                                 String moreStr = tmpBkReader.readedXElement.getAttrValue("MORE");
//                                 if (StringHelper.hasAnyChar(moreStr) && moreStr.equals("Y")) {
//                                     more = true;
//                                     page++;
//                                 }
//                             }
//                             ArrayList<XElement> list = tmpBkReader.readedXElement.getChildList("CONTENT");
//                             for (XElement xElement : list) {
//                                 <RSBODY MORE="N">	MORE是否有下一页，Y有，N无	非必回
//                                 <CONTENT>
//                                 <BUSINESSTYPE/>	业务标识码, 2位
//                                 见:附录-电子票据汇票系统业务类型对照表
//                                         <OPERTYPE/>	发起方标志
//                                 01:发起方/02接收方/03:持票人/04:全部
//
//                                 <ACPTR>	承兑人信息
//                                         <CUSTTYPE/>	客户类型
//                                         <ACCTID/>	账户代号
//                                         <NAME/>	账户名称
//                                         <BANKNUM/>	开户行行号
//                                         <ORGID/>	组织机构代码
//                                         </ACPTR>
//
//                                         <DTTRN/>	交易日期,YYYY-MM-DD
//                                         <APPLYMEMO/>	申请人备注	非必回
//                                         <RECEIVEMEMO/>	签收人备注	非必回
//                                 <DEALSTATUS/>	处理结果
//                                 对于发送方：
//                                 01-已录入
//                                 02-已发送
//                                 03-发送已确认(已收到ECDS应答)
//                                 04-发送已确认失败
//                                 05-已签收
//                                 06-已拒绝
//                                 07-已清算
//                                 08-已清算失败
//                                 对于接收方：
//                                 01-未签收
//                                 02-已签收
//                                 03-已拒绝
//                                 04-已确认
//                                 05-驳回已确认
//                                 06-已清算
//                                 07-已清算失败
//                                 08-已清分失败	非必回
//                                         </CONTENT>
//                                 </RSBODY>
//                                 </EBSTMTQUERYTRNRS>
//                                 </SECURITIES_MSGSRSV1>

//                                 RetAtsB002BillTransQryDetail detail = new RetAtsB002BillTransQryDetail();
//                                 //电子票据号码
//                                 detail.billNo =xElement.getSingleText("BILLCODE");
//                                 //票据类型 1-银承 2-商承
//                                 String billType = xElement.getSingleText("BILLTYPE");
//                                 if("AC01".equals(billType))
//                                 {
//                                     detail.billType ="1";
//                                 }else if("AC02".equals(billType)){
//                                     detail.billType="2";
//                                 }
//
//                                 //操作标志 1-出票 2-承兑 3-收票 4-背书转让 5-质押 6-解质押 7-出票保证 8-承兑保证 9-背书保证 10-贴现 11-回购式贴现赎回 12-提示付款 13-追索 14-同意清偿
//                                 detail.busiType = query.busiType;
////    查询类型 1-申请 2-应答 3-撤回
//                                 detail.queryType = query.queryType;
//
//                                 //申请日期
//                                 detail.applyDate = xElement.getSingleText("DTTRN");
//                                 //申请人
//                                 detail.applyName = xElement.getSingleText("/ACCTFROM/NAME");
//                                 //申请人组织机构代码
//                                 detail.applyCode =xElement.getSingleText("/ACCTFROM/ACCTID");
//                                 //申请人账号
//                                 detail.applyAct =xElement.getSingleText("/ACCTFROM/ACCTID");
//                                 //申请人开户行行名
//                                 detail.applyBnkName=xElement.getSingleText("/ACCTFROM/ACCTID");
//                                 //申请人开户行行号
//                                 detail.applyBnkCode=xElement.getSingleText("/ACCTFROM/ACCTID");
//
//                                 //收款人
//                                 detail.receName=xElement.getSingleText("/ACCTFROM/ACCTID");
//                                 //收款人组织机构代码
//                                 detail.receCode=xElement.getSingleText("/ACCTFROM/ACCTID");
//                                 //收款人人账号
//                                 detail.receAct=xElement.getSingleText("/ACCTFROM/ACCTID");
//                                 //收款人人开户行行名
//                                 detail.receBnkName=xElement.getSingleText("/ACCTFROM/ACCTID");
//                                 //收款人开户行行号
//                                 detail.receBnkCode=xElement.getSingleText("/ACCTFROM/ACCTID");
//
//                                 //备注
//                                 detail.memo=xElement.getSingleText("/ACCTFROM/ACCTID");
//
//                                 detail.billNo = xElement.getSingleText("BILLCODE");
//                                 retAts.writeDetail(detail);
//
//
//                             }
//                         } else {
//                             respCode = "-1";
//                             respInfo = returnMsg;
//                         }
//                     } else {
//                         respCode = "-1";
//                         respInfo = tmpSbmsg.toString();
//                     }
//                 } else {
//                     respCode = "-1";
//                     respInfo = result[1];
//                 }
//             }
//        }catch (Exception ex){
//        ex.printStackTrace();
//        respCode ="-1";
//        respInfo =ex.getMessage();
//        }
//
//        return null;
        return null;
    }

    //票据正面信息查询
    @Override
    public RetAtsB004BillFrontQry D_B004_BillFront_Query(ReqB004BillFrontQry query) throws Exception {
        RetAtsB004BillFrontQry retAts = new RetAtsB004BillFrontQry(query.reqHeader);
        retAts.writeStart();
        String respCode = "";
        String respInfo = "";
        try {
            if (query.readRecord()) {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                xmlWriter.autoFormat = false;
                writeCommonHeaderEn(xmlWriter, "encoding=\"gb2312\"");
//            < SECURITIES_MSGSRQV1>
                xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
//            <EBFACEQUERYTRNRQ>
                xmlWriter.writeStartElement("EBFACEQUERYTRNRQ");
//            <TRNUID/>	客户端交易的唯一流水号
                String trnuId = getQryTransID();
                xmlWriter.writeElementString("TRNUID", trnuId);
//            建议值为YYYYMMDD+序号，最长30位	必输
//            <RQBODY>		必输
                xmlWriter.writeStartElement("RQBODY");
//                    <BILLCODE/>	票据号码,30位	必输
                xmlWriter.writeElementString("BILLCODE", query.detail.billNo);
//            <ACCTID/>	操作员账户代号,最大32位	必输
                xmlWriter.writeElementString("ACCTID", query.detail.actNum);
//                    </RQBODY>
//            </EBFACEQUERYTRNRQ>
//            </ SECURITIES_MSGSRQV1>
                xmlWriter.writeEndDocument();
                String xmlSend = writeContentLength(xmlWriter);
                String[] result = sendToBank(xmlSend);
                if (result[0].length() == 0) {
                    String returnXml = result[1].substring(result[1].indexOf("<FOX>"), result[1].length());
                    StringBuilder tmpSbmsg = new StringBuilder();
                    XmlTextReader tmpbkReader = loadBankRespXmlReader(returnXml, tmpSbmsg);
                    if (tmpbkReader != null) {
                        String resultCode = tmpbkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBFACEQUERYTRNRS/STATUS/CODE");
                        String resultMsg = tmpbkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBFACEQUERYTRNRS/STATUS/MESSAGE");
                        if ("0".equals(resultCode)) {
                            if (tmpbkReader.read("/FOX/SECURITIES_MSGSRSV1/EBFACEQUERYTRNRS/RSBODY")) {
                                retAts.respCode = "0";
                                retAts.respInfo = resultMsg;
//                                    <ENTRSPAY/>	到期无条件支付委托 CC00
//                            <PROMSPAY/>	到期无条件支付承诺 CC00
                                retAts.mustPay = "CC00".equals(tmpbkReader.readedXElement.getSingleText("ENTRSPAY")) ? "1" : "0";
                                //电子票据
                                retAts.billNo = tmpbkReader.readedXElement.getSingleText("BILLCODE");
                                //票据类型 1-银承 2-商承
                                String billType2 = tmpbkReader.readedXElement.getSingleText("BILLTYPE");
                                if ("AC01".equals(billType2)) {
                                    retAts.billType = "1";
                                } else if ("AC02".equals(billType2)) {
                                    retAts.billType = "2";
                                }
                                //出票日
                                retAts.issDate = tmpbkReader.readedXElement.getSingleText("DRAWDATE");
                                //到期日
                                retAts.dueDate = tmpbkReader.readedXElement.getSingleText("DUEDATE");
                                //电子票据金额
                                retAts.billAmt = tmpbkReader.readedXElement.getSingleTextNotNull("BILLAMT");
                                //背书转让标识 1-可再转让 0-不得转让
                                String transFlag = tmpbkReader.readedXElement.getSingleText("NEGTBL");
                                if ("EM00".equals(transFlag)) {
                                    retAts.transFlag = "1";
                                } else if ("EM01".equals(transFlag)) {
                                    retAts.transFlag = "0";
                                }
//                        //承兑无条件支付委托 1-无条件支付 0-非条件支付
//                        retAts.mustPay;
                                //出票人
                                retAts.drawName = tmpbkReader.readedXElement.getSingleTextNotNull("DRAWER/NAME");
                                //出票人账号
                                retAts.drawAct = tmpbkReader.readedXElement.getSingleTextNotNull("DRAWER/ACCTID");

                                //出票人开户行行名
                                retAts.drawBnkName = tmpbkReader.readedXElement.getSingleTextNotNull("DRAWER/BANKDESC");
                                //出票人开户行行号
                                retAts.drawBnkCode = tmpbkReader.readedXElement.getSingleTextNotNull("DRAWER/BANKNUM");
                                //出票人信用等级
                                retAts.creditLv = tmpbkReader.readedXElement.getSingleTextNotNull("DRAWER/CREDITINFO/CRDLVL");
                                //出票人信用评级机构
                                retAts.gradeAgent = tmpbkReader.readedXElement.getSingleTextNotNull("DRAWER/CREDITINFO/RTGAGNCY");
                                //出票人评级到期日
                                retAts.gradeDueDate = tmpbkReader.readedXElement.getSingleTextNotNull("DRAWER/CREDITINFO/RTGDUEDATE");
                                //承兑人名称
                                retAts.accpName = tmpbkReader.readedXElement.getSingleTextNotNull("ACPTR/NAME");
                                //承兑人账号
                                retAts.accpAct = tmpbkReader.readedXElement.getSingleTextNotNull("ACPTR/ACCTID");
                                //承兑人开户行行名
                                retAts.accpBnkName = tmpbkReader.readedXElement.getSingleTextNotNull("ACPTR/BANKDESC");
                                //承兑人开户行行号
                                retAts.accpBnkCode = tmpbkReader.readedXElement.getSingleTextNotNull("ACPTR/BANKNUM");
                                //承兑人信用等级
                                retAts.accpCredit = tmpbkReader.readedXElement.getSingleTextNotNull("ACPTR/CREDITINFO/CRDLVL");
                                //承兑人评级机构
                                retAts.accpGrade = tmpbkReader.readedXElement.getSingleTextNotNull("/ACPTR/CREDITINFO/RTGAGNCY");
                                //承兑人评级到期日
                                retAts.accpGradeDue = tmpbkReader.readedXElement.getSingleTextNotNull("ACPTR/CREDITINFO/RTGDUEDATE");
                                //收款人名称
                                retAts.payeeName = tmpbkReader.readedXElement.getSingleTextNotNull("PAYEE/NAME");
                                //收款人账号
                                retAts.payeeAct = tmpbkReader.readedXElement.getSingleTextNotNull("PAYEE/ACCTID");

                                //收款人开户行行名
                                retAts.payeeBnkName = tmpbkReader.readedXElement.getSingleTextNotNull("PAYEE/BANKDESC");
                                //收款人开户行行号
                                retAts.payeeBnkCode = tmpbkReader.readedXElement.getSingleTextNotNull("PAYEE/BANKNUM");
                                //票据状态
                                retAts.billState = tmpbkReader.readedXElement.getSingleTextNotNull("BILLSTATUS");
                                retAts.writeRetDetail();
                            } else {
                                throw new Exception("报文中没有票据正面信息的内容");
                            }

                        } else {
                            respCode = "-1";
                            respInfo = resultCode + " - " + resultMsg;
                        }
                    } else {
                        respCode = "-1";
                        respInfo = tmpbkReader.toString();
                    }
                } else {
                    respCode = "-1";
                    respInfo = result[1];
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            respCode = "-1";
            respInfo = ex.getMessage();
        }
        if ("-1".equals(respCode)) {
            retAts = new RetAtsB004BillFrontQry(query.reqHeader);
            retAts.writeStart();
            retAts.respCode = respCode;
            retAts.respInfo = respInfo;
            retAts.writeRetDetail();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB005BillBackQry D_B005_BillBack_Query(ReqB005BillBackQry query) throws Exception {
        RetAtsB005BillBackQry retAts = new RetAtsB005BillBackQry(query.reqHeader);
        retAts.writeStart();
        retAts.writeRespCode("0");
        retAts.writeRespInfo("");
        query.readRecord();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            xmlWriter.autoFormat = false;
            writeCommonHeaderEn(xmlWriter, "encoding=\"gb2312\"");
//                    < SECURITIES_MSGSRQV1>		必输
            xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
//                            <EBBACKQUERYTRNRQ>		必输
            xmlWriter.writeStartElement("EBBACKQUERYTRNRQ");
//                            <TRNUID/>	客户端交易的唯一流水号，
//                    建议值为YYYYMMDD+序号，最长30位	必输
            String trnuId = getQryTransID();
            xmlWriter.writeElementString("TRNUID", trnuId);
//                    <RQBODY PAGE=“1”>	PAGE查询页码，必输	必输
            xmlWriter.writeElementAttribute("RQBODY", "PAGE", "1");
//                    <BILLCODE/>	票据号码,30位	必输
            xmlWriter.writeElementString("BILLCODE", query.detail.billNo);
//                    <ACCTID/>	操作员账户代号,最大32位	必输
            xmlWriter.writeElementString("ACCTID", query.detail.actNum);
//                            </RQBODY>
//                    </EBBACKQUERYTRNRQ>
//                    </ SECURITIES_MSGSRQV1>
            xmlWriter.writeEndDocument();
            String xmlSend = writeContentLength(xmlWriter);
            String[] result = sendToBank(xmlSend);
            if (result[0].length() == 0) {
                String returnXml = result[1].substring(result[1].indexOf("<FOX>"), result[1].length());
                StringBuilder tmpSbmsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbmsg);
                if (tmpBkReader != null) {
                    String returnCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBBACKQUERYTRNRS/STATUS/CODE");
                    String returnMsg = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBBACKQUERYTRNRS/STATUS/MESSAGE");
                    if ("0".equals(returnCode)) {
                        while (tmpBkReader.read("/FOX/SECURITIES_MSGSRSV1/EBBACKQUERYTRNRS/RSBODY/CONTENT")) {
                            RetAtsB005BillBackQryDetail detail = new RetAtsB005BillBackQryDetail();
                            XElement xElement = tmpBkReader.readedXElement;
                            detail.respCode = "0";
//                            <BUSINESSTYPE/>	流转类别
                            String transType = xElement.getSingleText("BUSINESSTYPE");
                            if ("03".equals(transType)) {
                                detail.transCode = "2";
                            } else if ("05".equals(transType)) {
                                detail.transCode = "3";
                            } else if ("07".equals(transType)) {
                                detail.transCode = "10";
                            } else if ("09".equals(transType)) {
                                detail.transCode = "11";
                            } else if ("10".equals(transType)) {
                                detail.transCode = "12";
                            } else if ("11".equals(transType)) {
                                detail.transCode = "13";
                            } else if ("12".equals(transType)) {
                                detail.transCode = "14";
                            } else if ("13".equals(transType)) {
                                detail.transCode = "15";
                            } else if ("14".equals(transType)) {
                                detail.transCode = "16";
                            } else if ("15".equals(transType)) {
                                detail.transCode = "18";
                            } else if ("16".equals(transType)) {
                                detail.transCode = "18";
                            } else if ("17".equals(transType)) {
                                detail.transCode = "20";
                            } else if ("18".equals(transType)) {
                                detail.transCode = "21";
                            } else if ("19".equals(transType) || "20".equals(transType)) {
                                detail.transCode = "22";
                            } else if ("21".equals(transType)) {
                                detail.transCode = "23";
                            } else {
                                continue;
                            }
                            detail.billNo = query.detail.billNo;
//                                    <SIGNDATE/>	签收日期,YYYY-MM-DD
                            detail.respDate = xElement.getSingleText("SIGNDATE");
//                                    <APPDATE/>	申请日期,YYYY-MM-DD
                            detail.applyDate = xElement.getSingleText("APPDATE");
//                                    <PROPSR>	申请人信息
//                                    <NAME/>	账户名称,最大60位
                            detail.applyName = xElement.getSingleText("PROPSR/NAME");
//                            <RCVRR>	接收人信息
//                                    <NAME/>	账户名称,最大60位
                            detail.receName = xElement.getSingleText("RCVRR/NAME");
//                                    (EM00:可再转让,EM01:不可再转让)
                            String transflag = xElement.getSingleText("NEGTBL");
                            if ("EM00".equals(transflag)) {
                                detail.transFlag = "1";
                            } else if ("EM01".equals(transflag)) {
                                detail.transFlag = "0";
                            }
                            retAts.writeDetail(detail);
                        }
                    } else {
                        throw new Exception(returnMsg);
                    }
                } else {
                    throw new Exception(tmpSbmsg.toString());
                }
            } else {
                throw new Exception(result[1]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsB005BillBackQry(query.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(ex.getMessage());
        }
        retAts.writeEnd();
        return retAts;
    }

    //兴业银行票据池BP008和电票用的同样的接口，修改的话两个都要修改
    @Override
    public RetAtsB006BillStateQry D_B006_BillState_Qeury(ReqB006BillStateQry query) throws Exception {
        if (getBankConfig().getValueAsBool("useNewBillStateQry")) {
            return billStateQeuryByNewInterface(query);
        } else {
            return BillState_Qeury(query);
        }
    }
    //票据交易状态查询（电票和票据池BP001使用的同一个接口，一个改都要改）
    public RetAtsB006BillStateQry BillState_Qeury(ReqB006BillStateQry query) throws Exception {
        RetAtsB006BillStateQry retAts = new RetAtsB006BillStateQry(query.reqHeader);
        retAts.writeStart();
        Connection conn = FGBiz.getLocalDbConn();
        while (query.readRecord()) {
            RetAtsB006BillStateQryDetail detail = new RetAtsB006BillStateQryDetail();
            try {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                xmlWriter.autoFormat = false;
                writeCommonHeaderEn(xmlWriter, "encoding=\"gb2312\"");
//                    < SECURITIES_MSGSRQV1>		必输
                xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
//                            <EBBATCHSTATUSTRNRQ>		必输
                xmlWriter.writeStartElement("EBBATCHSTATUSTRNRQ");
//                            <CLIENTREF/>	客户交易流水号
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTRECEIPT, new Object[]{query.detail.reqSeqID, query.reqHeader.bankCode});
                if (map==null || map.size() == 0) {
                    detail.respCode = "0";
                    detail.respInfo = "没有找到银行流水号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
//                            <TRNUID/>	客户端交易的唯一流水号
                String srnuId = getQryTransID();
                xmlWriter.writeElementString("TRNUID", map.get("BANKREQSEQID").toString());
//                xmlWriter.writeElementString("TRNUID", srnuId);
//                    建议值为YYYYMMDD+序号，最长30位	必输
//                    <RQBODY PAGE=“1”>	PAGE查询页码，必输
                xmlWriter.writeElementAttribute("RQBODY", "PAGE", "1");
//                            <ACCTID/>	操作员账户代号,最大32位	必输
                xmlWriter.writeElementString("ACCTID", query.detail.actNum);
                String outTransId = map.get("BANKREQSEQID").toString();
//                    String transDate = map.get("TRANSDATE").toString();
                xmlWriter.writeElementString("CLIENTREF", outTransId);
//                            </RQBODY>
//                    </EBBATCHSTATUSTRNRQ>
//                    </ SECURITIES_MSGSRQV1>
                xmlWriter.writeEndDocument();
                String xmlSend = writeContentLength(xmlWriter);
                String[] result = sendToBank(xmlSend);
                if (result[0].length() == 0) {
                    String returnXml = result[1].substring(result[1].indexOf("<FOX>"), result[1].length());
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBBATCHSTATUSTRNRS/STATUS/CODE");
                        String returnMsg = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBBATCHSTATUSTRNRS/STATUS/MESSAGE");
                        if ("0".equals(returnCode)) {
                            if (tmpBkReader.read("/FOX/SECURITIES_MSGSRSV1/EBBATCHSTATUSTRNRS/RSBODY")) {
                                ArrayList<XElement> list = tmpBkReader.readedXElement.getChildList("CONTENT");
                                if (list == null || list.size() == 0 || list.size() != 1) {
                                    throw new Exception("返回的记录条数不唯一返回条数为：" + (list != null ? list.size() : "0"));
                                }
                                for (XElement xElement : list) {
                                    detail.respCode = "0";
                                    detail.respInfo = xElement.getSingleText("RSLTMSG");
                                    //电子票据号码
                                    detail.billNo = xElement.getSingleText("BILLCODE");
                                    //银行交易处理状态码
                                    detail.transState = xElement.getSingleText("RSLTID");
                                    //银行交易信息码
                                    detail.payInfoCode = detail.transState;
                                    //票据状态
                                    detail.billState = xElement.getSingleText("BILLSTATUS");
                                }
                            } else {
                                detail.respCode = "-1";
                                detail.respInfo = "返回的内容没有状态信息";
                            }
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = returnMsg;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            } catch (Exception e) {
                detail.respCode = "-1";
                detail.respCode = e.getMessage();
                e.printStackTrace();
            } finally {
                conn.close();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }
    public RetAtsB006BillStateQry billStateQeuryByNewInterface(ReqB006BillStateQry query) throws Exception {
        RetAtsB006BillStateQry retAts = new RetAtsB006BillStateQry(query.reqHeader);
        retAts.writeStart();
        Connection conn = FGBiz.getLocalDbConn();
        while (query.readRecord()) {
            RetAtsB006BillStateQryDetail detail = new RetAtsB006BillStateQryDetail();
            try {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                xmlWriter.autoFormat = false;
                writeCommonHeaderEn(xmlWriter, "encoding=\"gb2312\"");
//                    < SECURITIES_MSGSRQV1>		必输
                xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
//                            <EBILLTRANSRESULTQUERYTRNRQ>		必输
                xmlWriter.writeStartElement("EBILLTRANSRESULTQUERYTRNRQ");
                Map<String, Object> map = JDBCHelper.execQuerySingle(conn, SELECTRECEIPT, new Object[]{query.detail.reqSeqID,query.reqHeader.bankCode});
                if (map==null || map.size() == 0) {
                    detail.respCode = "0";
                    detail.respInfo = "没有找到流水号对应的票据ID号！";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
//                            <TRNUID/>	客户端交易的唯一流水号
                xmlWriter.writeElementString("TRNUID", getQryTransID());
//                xmlWriter.writeElementString("TRNUID", srnuId);
//                    建议值为YYYYMMDD+序号，最长30位	必输
//                    <RQBODY PAGE=“1”>	PAGE查询页码，必输
                xmlWriter.writeStartElement("RQBODY");
//                            <ACCTID/>	操作员账户代号,最大32位	必输
//                xmlWriter.writeElementString("ACCTID", query.detail.actNum);
//                    String transDate = map.get("TRANSDATE").toString();
                xmlWriter.writeElementString("CLIENTREF", map.get("BANKREQSEQID").toString());
//                            </RQBODY>
//                    </EBBATCHSTATUSTRNRQ>
//                    </ SECURITIES_MSGSRQV1>
                xmlWriter.writeEndDocument();
                String xmlSend = writeContentLength(xmlWriter);
                String[] result = sendToBank(xmlSend);
                if (result[0].length() == 0) {
                    String returnXml = result[1].substring(result[1].indexOf("<FOX>"));
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBILLTRANSRESULTQUERYTRNRS/STATUS/CODE");
                        String returnMsg = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBILLTRANSRESULTQUERYTRNRS/STATUS/MESSAGE");
                        if ("0".equals(returnCode)) {
                            if (tmpBkReader.read("/FOX/SECURITIES_MSGSRSV1/EBILLTRANSRESULTQUERYTRNRS/RSBODY")) {
                                ArrayList<XElement> list = tmpBkReader.readedXElement.getChildList("CONTENT");
                                if (list == null || list.size() == 0 || list.size() != 1) {
                                    throw new Exception("返回的记录条数不唯一返回条数为：" + (list != null ? list.size() : "0"));
                                }
                                for (XElement xElement : list) {
                                    detail.respCode = "0";
                                    detail.respInfo = returnMsg;
                                    //电子票据号码
                                    detail.billNo = xElement.getSingleText("BILL_CODE");
                                    //银行交易处理状态码
                                    detail.transState = xElement.getSingleText("TRANS_STATUS");
                                    //银行交易信息码
                                    detail.payInfoCode = detail.transState;
                                    //票据状态
                                    detail.billState = getBillStateByBillNo(detail.billNo, query.detail.actNum);
                                }
                            } else {
                                detail.respCode = "-1";
                                detail.respInfo = "返回的内容没有状态信息";
                            }
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = returnMsg;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = result[1];
                }
            } catch (Exception e) {
                detail.respCode = "-1";
                detail.respCode = e.getMessage();
                e.printStackTrace();
            } finally {
                conn.close();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB007BillKzsrQry D_B007_BillKzsr_Query(ReqB007BillKzsrQry query) throws Exception {
        RetAtsB007BillKzsrQry retAts = new RetAtsB007BillKzsrQry(query.reqHeader);
        retAts.writeStart();
        String respCode = "";
        String respInfo = "";
        try {
            if (query.readRecord()) {
                boolean more = true;
                int page = 1;
                while (more) {
                    more = false;
                    XmlTextWriter xmlWriter = new XmlTextWriter();
                    xmlWriter.autoFormat = false;
                    writeCommonHeaderEn(xmlWriter, "encoding=\"gb2312\"");
//            < SECURITIES_MSGSRQV1>		必输
                    xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
//                    <EBRECRQUERYTRNRQ>		必输
                    xmlWriter.writeStartElement("EBRECRQUERYTRNRQ");
//                    <TRNUID/>	客户端交易的唯一流水号
                    String trnuId = getQryTransID();
                    xmlWriter.writeElementString("TRNUID", trnuId);
//            建议值为YYYYMMDD+序号，最长30位	必输
//            <RQBODY PAGE=“1”>	PAGE查询页码，必输	必输
                    xmlWriter.writeElementAttribute("RQBODY", "PAGE", page + "");
//            <BILLCODE/>	票据号码,30位	必输
                    xmlWriter.writeElementString("BILLCODE", query.detail.billNo);
//            <ACCTID/>	操作员账户代号,最大32位	必输
                    xmlWriter.writeElementString("ACCTID", query.detail.actNum);
//                    </RQBODY>
//            </EBRECRQUERYTRNRQ>
//            </ SECURITIES_MSGSRQV1>
                    xmlWriter.writeEndDocument();
                    String xmlSend = writeContentLength(xmlWriter);
                    String[] result = sendToBank(xmlSend);
                    if (result[0].length() == 0) {
                        String returnXml = result[1].substring(result[1].indexOf("<FOX>"), result[1].length());
                        StringBuilder tmpSbmsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbmsg);
                        if (tmpBkReader != null) {
                            String returnCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBRECRQUERYTRNRS/STATUS/CODE");
                            String returnMsg = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBRECRQUERYTRNRS/STATUS/MESSAGE");
                            if ("0".equals(returnCode)) {
                                if (page == 1) {
                                    retAts.respCode = "0";
                                    retAts.respInfo = returnMsg;
                                    retAts.billNo = query.detail.billNo;
                                    retAts.writeRespInfos();
                                }
                                if (tmpBkReader.read("/FOX/SECURITIES_MSGSRSV1/EBRECRQUERYTRNRS/RSBODY")) {
                                    String moreStr = tmpBkReader.readedXElement.getAttrValue("MORE");
                                    if (StringHelper.hasAnyChar(moreStr) && moreStr.equals("Y")) {
                                        more = true;
                                        page++;
                                    }
                                }
                                ArrayList<XElement> list = tmpBkReader.readedXElement.getChildList("COMTENT");
                                if (list != null) {
                                    for (XElement xElement : list) {
                                        RetAtsB007BillKzsrQryDetail detail = new RetAtsB007BillKzsrQryDetail();
                                        detail.receAct = xElement.getSingleText("ACCTID");
                                        detail.receName = xElement.getSingleText("NAME");
                                        detail.receCode = xElement.getSingleText("ORGID");
                                        detail.receBnkCode = xElement.getSingleText("BANKNUM");
                                        detail.receBnkName = xElement.getSingleText("BANKDESC");
                                        retAts.writeDetail(detail);
                                    }
                                }
                            } else {
                                respCode = "-1";
                                respInfo = returnMsg;
                            }
                        } else {
                            respCode = "-1";
                            respInfo = tmpSbmsg.toString();
                        }
                    } else {
                        respCode = "-1";
                        respInfo = result[1];
                    }

                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            respCode = "-1";
            respInfo = ex.getMessage();
        }
        if ("-1".equals(respCode)) {
            retAts = new RetAtsB007BillKzsrQry(query.reqHeader);
            retAts.writeStart();
            retAts.respCode = "-1";
            retAts.respInfo = respInfo;
            retAts.writeRespInfos();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB008HistoricalBillQry D_B008_HistoricalBillQuery(ReqB008HistoricalBillQry query) throws Exception {
        RetAtsB008HistoricalBillQry retAts = new RetAtsB008HistoricalBillQry(query.reqHeader);
        retAts.writeStart();
        String respCode = "";
        String respInfo = "";
        try {
            while (query.readRecord() && !"-1".equals(respCode)) {
                int page = 0;
                boolean more = true;
                while (more && !"-1".equals(respCode)) {
                    more = false;
                    page++;
                    XmlTextWriter xmlWriter = new XmlTextWriter();
                    xmlWriter.autoFormat = false;
                    writeCommonHeaderEn(xmlWriter, "encoding=\"gb2312\"");
//                < SECURITIES_MSGSRQV1>		必输
                    xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
//                        <EBSTMTQUERYTRNRQ>		必输
                    xmlWriter.writeStartElement("EBSTMTQUERYTRNRQ");
//                        <TRNUID/>	客户端交易的唯一流水号
                    String trunId = getQryTransID();
                    xmlWriter.writeElementString("TRNUID", trunId);
//                建议值为YYYYMMDD+序号，最长30位	必输
//                <RQBODY PAGE=“1”>	PAGE查询页码，必输	必输
                    xmlWriter.writeElementAttribute("RQBODY", "PAGE", "" + page);
//                <OPERTYPE/>	发起方标志
//                01:发起方/02接收方/03:持票人/04:全部	必输
                    if ("1".equals(query.detail.queryType)) {
                        xmlWriter.writeElementString("OPERTYPE", "01");
                    } else {
                        xmlWriter.writeElementString("OPERTYPE", "02");
                    }
//                <ACCTID/>	32位操作员账户代号	必输
                    xmlWriter.writeElementString("ACCTID", query.detail.actNum);
//                <BUSINESSTYPE/>	业务标识码,2位,见:附录3-业务类型对照表	必输
//                    * 业务类型 业务类型 1-提示收票 2-背书转让 3-贴现 10-质押
                    //  05   07  09  15
                    String businessType;
                    if ("1".equals(query.detail.busiType)) {
                        businessType = "05";
                    } else if ("2".equals(query.detail.busiType)) {
                        businessType = "07";
                    } else if ("3".equals(query.detail.busiType)) {
                        businessType = "09";
                    } else if ("4".equals(query.detail.busiType)) {
                        businessType = "15";

                    } else {
                        throw new Exception("未知的业务类型:" + query.detail.busiType);
                    }
                    xmlWriter.writeElementString("BUSINESSTYPE", businessType);
//                        <BILLTYPE/>	票据类型(AC01:银票,AC02:商票)	非必输
                    String billType;
                    if ("1".equals(query.detail.billType)) {
                        billType = "AC01";
                    } else {
                        billType = "AC02";
                    }
                    xmlWriter.writeElementString("BILLTYPE", billType);
//                        <BILLCODE/>	票据号码,30位	非必输
                    xmlWriter.writeElementString("BILLCODE", query.detail.billNo, true);
//                <DTSTART/>	起始日期YYYY-MM-DD	必输
                    xmlWriter.writeElementString("DTSTART", DateHelper.convertD8toD10(query.detail.dueDateDown));
//                        <DTEND/>	截止日期YYYY-MM-DD	必输
                    xmlWriter.writeElementString("DTEND", DateHelper.convertD8toD10(query.detail.dueDateUp));
//                </RQBODY>
//                </EBSTMTQUERYTRNRQ>
//                </ SECURITIES_MSGSRQV1>
                    xmlWriter.writeEndDocument();
                    String xmlSend = writeContentLength(xmlWriter);
                    String[] result = sendToBank(xmlSend);
                    if (result[0].length() == 0) {
                        String returnXml = result[1].substring(result[1].indexOf("<FOX>"), result[1].length());
                        StringBuilder tmpSbmsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbmsg);
                        if (tmpBkReader != null) {
                            String returnCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBSTMTQUERYTRNRS/STATUS/CODE");
                            String returnMsg = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/EBSTMTQUERYTRNRS/STATUS/MESSAGE");
                            if ("0".equals(returnCode)) {
                                if (tmpBkReader.read("/FOX/SECURITIES_MSGSRSV1/EBSTMTQUERYTRNRS/RSBODY")) {
                                    String moreStr = tmpBkReader.readedXElement.getAttrValue("MORE");
                                    if (StringHelper.hasAnyChar(moreStr) && moreStr.equals("Y")) {
                                        more = true;
                                        page++;
                                    }
                                }
                                if (StringHelper.isNullOrEmpty(retAts.respCode)) {
                                    retAts.respCode = "0";
                                    retAts.respInfo = returnMsg;
                                }
                                ArrayList<XElement> list = tmpBkReader.readedXElement.getChildList("CONTENT");
                                if (list != null) {
                                    for (XElement xElement : list) {
//                                    <SECURITIES_MSGSRSV1>	响应服务指令消息集	必回
//                                    <CONTENT>
//                                    <BUSINESSTYPE/>	业务标识码, 2位
//                                    见:附录-电子票据汇票系统业务类型对照表
//                                            <OPERTYPE/>	发起方标志
//                                    01:发起方/02接收方/03:持票人/04:全部
//

                                        RetAtsB008HistoricalBillQryDetail detail = new RetAtsB008HistoricalBillQryDetail();

                                        detail.billNo = xElement.getSingleText("BILLCODE");
                                        String billType2 = xElement.getSingleText("BILLTYPE");
                                        if ("AC01".equals(billType2)) {
                                            detail.billType = "1";
                                        } else if ("AC02".equals(billType2)) {
                                            detail.billType = "2";
                                        }
                                        detail.issDate = xElement.getSingleText("DRAWDATE");
                                        detail.dueDate = xElement.getSingleText("DUEDATE");
                                        detail.billAmt = xElement.getSingleText("BILLAMT");
                                        detail.drawName = xElement.getSingleText("DRAWER/NAME");
                                        detail.drawCode = xElement.getSingleText("DRAWER/ORGID");
                                        detail.drawAct = xElement.getSingleText("DRAWER/ACCTID");
                                        detail.drawBnkCode = xElement.getSingleText("DRAWER/BANKNUM");
                                        detail.accpName = xElement.getSingleText("ACPTR/NAME");
                                        detail.accpCode = xElement.getSingleText("ACPTR/ORGID");
                                        detail.accpAct = xElement.getSingleText("ACPTR/ACCTID");
                                        detail.accpBnkCode = xElement.getSingleText("ACPTR/BANKNUM");

                                        /**
                                         * 操作标志
                                         */
                                        detail.busiType = query.detail.busiType;
                                        /**
                                         * 查询类型
                                         */
                                        detail.queryType = query.detail.queryType;
                                        retAts.writeDetail(detail);
                                    }
                                }
                            } else {
                                respCode = "-1";
                                respInfo = returnMsg;
                            }
                        } else {
                            respCode = "-1";
                            respInfo = tmpSbmsg.toString();
                        }
                    } else {
                        respCode = "-1";
                        respInfo = result[1];
                    }
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
            respCode = "-1";
            respInfo = ex.getMessage();
        }
        if ("-1".equals(respCode)) {
            retAts = new RetAtsB008HistoricalBillQry(query.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(respInfo);
        }
        retAts.writeAfter();
        return retAts;
    }

    @Override
    public RetAtsB009DrawBillQry D_B009_DrawBillQuery(ReqB009DrawBillQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB010ProtocolStateQry D_B010_ProtocolStateQuery(ReqB010ProtocolStateQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB011TransFlagQry D_B011_TransFlagQuery(ReqB011TransFlagQry query) throws Exception {
        return null;
    }
}
